//
//  NewsDetailViewController.m
//  GentlemanAnime
//
//  Created by dllo on 15/6/30.
//  Copyright (c) 2015年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
#import "DownloadSingletonTool.h"
#import "AppDelegate.h"
#import "LeftCollectionViewCell.h"

@interface NewsDetailViewController ()

@property(nonatomic,retain)MBProgressHUD *SearchDetailHUD;

@property(nonatomic, assign)BOOL isArchiver;
@property(nonatomic, retain)NSMutableArray *newsArr;

@end

@implementation NewsDetailViewController

- (void)dealloc
{
    [_SearchDetailHUD release];
    [_newsID release];
    [_newsArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titlelebel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    titlelebel.text = self.newsModel.title;
    titlelebel.font = [UIFont boldSystemFontOfSize:18];
    titlelebel.textAlignment = NSTextAlignmentCenter;
    titlelebel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titlelebel;
    [titlelebel release];

    // 反归档
    [self fanGuiDang];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    
    // 收藏按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30, 30);
    button.contentEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
    if (self.isArchiver) {
        [button setImage:[UIImage imageNamed:@"已收藏"] forState:UIControlStateNormal];
    }else{
        [button setImage:[UIImage imageNamed:@"收藏"] forState:UIControlStateNormal];
    }
    
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    // 小菊花
    self.SearchDetailHUD= [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //菊花的样式
    self.SearchDetailHUD.mode= MBProgressHUDModeIndeterminate;
    //菊花的颜色
    self.SearchDetailHUD.color = [UIColor colorWithRed:0.334 green:0.619 blue:1.000 alpha:1.000];

    NSString *strURL = [NSString stringWithFormat:@"http://www.manmi.me/?c=info&m=news_view&id=%@",self.newsID];
    NSURL *url = [NSURL URLWithString:strURL];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
   
    [webView release];
    //小菊花
    [self.SearchDetailHUD hide:YES];
}

#pragma mark 点击收藏
- (void)click:(UIButton *)button
{
    if (self.isArchiver) {
        NSInteger index = 0;
        for (NSInteger i = 0; i < self.newsArr.count; i++) {
            NewsTableViewModel *news = self.newsArr[i];
            if ([news.title isEqualToString:self.title]) {
                index = i;
                break;
            }
        }
        [self.newsArr removeObjectAtIndex:index];
        // 把修改过的数组再次进行归档
        // 进行归档
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        // 拼接路径
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"news.plist"];
        // 根据对应的路径进行归档
        [NSKeyedArchiver archiveRootObject:self.newsArr toFile:documentPath];
        [button setImage:[UIImage imageNamed:@"收藏"] forState:UIControlStateNormal];
        self.isArchiver = NO;
        
        
        [DownloadSingleton shareDownloadSingleton].collcetCount = [DownloadSingleton shareDownloadSingleton].collcetCount - 1;
        NSString *collcetCountBoxPath = [sandBoxPath stringByAppendingPathComponent:@"collcetCount.txt"];
        NSString *str = [NSString stringWithFormat:@"%d",[DownloadSingleton shareDownloadSingleton].collcetCount];
        [str writeToFile:collcetCountBoxPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        

    }else{
        // 进行归档操作
        [self.newsArr addObject:self.newsModel];
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"news.plist"];
        [NSKeyedArchiver archiveRootObject:self.newsArr toFile:documentPath];
        [button setImage:[UIImage imageNamed:@"已收藏"] forState:UIControlStateNormal];
        self.isArchiver = YES;
        
        [DownloadSingleton shareDownloadSingleton].collcetCount = [DownloadSingleton shareDownloadSingleton].collcetCount + 1;
        NSString *collcetCountBoxPath = [sandBoxPath stringByAppendingPathComponent:@"collcetCount.txt"];
        NSString *str = [NSString stringWithFormat:@"%d",[DownloadSingleton shareDownloadSingleton].collcetCount];
        [str writeToFile:collcetCountBoxPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    NSIndexPath *leftIndexPath = [DownloadSingleton shareDownloadSingleton].leftIndexPath;
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    LeftCollectionViewCell *cell = (LeftCollectionViewCell*)[tempAppDelegate.homeVC.collectionView cellForItemAtIndexPath:leftIndexPath];
    [cell.tableView reloadData];
}

#pragma mark 反归档
- (void)fanGuiDang
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"news.plist"];
    self.newsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    self.isArchiver = NO;
    if (self.newsArr == nil) {
        self.newsArr = [NSMutableArray array];
    }else{
        for (NewsTableViewModel *news in self.newsArr) {
            if ([news.Id isEqualToString:self.newsID]) {
                self.isArchiver = YES;
                break;
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
